<?php
define('APPGESTAPP','1.00');
/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

//carica la lingua se esiste
$lang=lang();
switch($lang){
    case "en":
	define("_ADMIN_PREFERENCE","ADMIN PREFERENCE");
	define("_CHANGE_PREFERENCES","change preferences");
	define("_CHANGE_PASSWORD","Change Password");
	define("_RETYPE_PASSWORD","Retype Password");
	define("_SET_PASSWORD","change password");
	define("_ADMINISTRATION_PRIVILEGE_REQUESTED", "administration privilege requested");
	define("_CHANGE","Change");
	define("_CONTINUA","Continue");
	break;
    default;
	define("_ADMIN_PREFERENCE","ADMIN PREFERENCE");
	define("_CHANGE_PREFERENCES","change preferences");
	define("_CHANGE_PASSWORD","Change Password");
	define("_SET_PASSWORD","change password");
	define("_RETYPE_PASSWORD","Retype Password");
	define("_ADMINISTRATION_PRIVILEGE_REQUESTED", "administration privilege requested");
	define("_CHANGE","Change");
	define("_CONTINUA","Continua");
	break;
}


zoom();

?>
<script type='text/javascript'>
//igrandisce al massimo l'area applicazione
function inout (){
    if(document.getElementById('treeboxbox_tree').style.display !='none'){
    document.getElementById('treeboxbox_tree').style.display='none';
    }
    else{
    document.getElementById('treeboxbox_tree').style.display='inline';
    }
}
</script>

<?

function makeform($randpath,$nf,$appname){
    echo "<h3>$randpath$nf</h3>\n";

// --------tabs---------------------------------------------------------------------------------------------------------

echo "<script type='text/javascript' src='js/tabs/tabpane.js'></script>";
echo "<link type='text/css' rel='StyleSheet' href='js/tabs/tabpane.css' />";

echo "<div class='tab-pane' id='tab-pane-1'>";

echo   "<div class='tab-page'>";
echo      "<h2 class='tab'>CODE</h2>";

echo    "File:<b>[</b>$randpath$nf<b>]</b>";
echo    "<hr />";
$file=$randpath.$nf;
//if (substr($nf, -4) != ".inc") {
//echo "<form action='index.php?appname=$appname&op=save&file=$randpath$nf/application.inc' method='post'>";
//} else {
echo "<form action='index.php?appname=$appname&op=save&file=$file' method='post'>";
//}

if (file_exists("extras/codepress/codepress.php")){
    echo "<input type='hidden' name='myfile' value=''>";
if (substr($nf, -4) != ".inc") {
      echo "Questa e' una cartella !!!";
//    echo "<iframe id='codepress' name='codepress' src='extras/codepress/codepress.php?action=edit&file=$randpath$nf/application.inc&language=php' width='100%' height='400'></iframe>";
} else {
    echo "<iframe id='codepress' name='codepress' src='extras/codepress/codepress.php?action=edit&file=$randpath$nf&language=php' width='100%' height='400'></iframe>";
    echo "<p align='right'><input type='submit' name='save' value='save code' onClick='myfile.value = CodePress.getCode();'></p>";
}
}
   echo "</div>";
echo "</div>";
echo "</form>";
// ---------------------------------------------------------------------------------------------------------------------
}


global $tree,$tree_i;
$tree_i=1;

function dirconfig($randpath,$myparent,$appname){
    global $tree,$tree_i;
    //$fd=glob($randpath."*");
    $fdh=opendir($randpath);
    while (false !== ($nf= readdir($fdh))){
	if ($nf[0]!='.' )
	$fd[$i++]=$nf;
    }
    closedir($fdh);

    if (count($fd)){
	sort($fd);
	foreach($fd as $nf){
	    $nf=basename($nf);
	    if ($nf[0]!='.' ){
		if (is_dir($randpath.$nf)){
		    if (file_exists($randpath.$nf)){
			$tree[$tree_i++]=$nf;
			echo "<script>tree.insertNewItem($myparent,$tree_i,\"$nf\",0,0,0,0,\"SELECT\");</script>\n ";
			echo"<div id='$tree_i' style=\"display:none;border:2;\" >\n";
			makeform($randpath,$nf,$appname);
			echo "</div>\n";
			dirconfig($randpath.$nf."/",$tree_i,$appname);
		    }
		}
		else{
		    if ($nf!="" && $nf!="" && substr($nf,-3)=="inc"){
			$tree[$tree_i++]=$nf;
			echo "<script>tree.insertNewItem($myparent,$tree_i,\"$nf\",0,0,0,0,\"SELECT\");</script>\n ";
			echo "<div id='$tree_i' style=\"display:none;border:2;\" >\n";
			makeform($randpath,$nf,$appname);
			echo "</div>\n";
		    }
		}
	    }
	}
    }
echo "<script>tree.closeAllItems(0);</script>\n";
}








?>
<?php if (admin_isadmin()) {
        admin_writelog(_ADMIN_PREFERENCE);
?>
    <p>


<style>
.fold
{
	border: 1px solid #808080;

}

</style>

<style>
	body {font-size:12px}
	.{font-family:arial;font-size:12px}
	h1 {cursor:hand;font-size:16px;margin-left:10px;line-height:10px}
	xmp {color:green;font-size:12px;margin:0px;font-family:courier;background-color:#e6e6fa;padding:2px}
</style>


<h3>FW - IDE</h3>
	<link rel="STYLESHEET" type="text/css" href="../css/dhtmlXTree.css">
	<script  src="js/dhtmlxTree/js/dhtmlXCommon.js"></script>
	<script  src="js/dhtmlxTree/js/dhtmlXTree.js"></script>

	<table border='1'>
		<tr>
			<td valign="top" >
		    <input type='image' src='applications/<?=$appname;?>/_images/icon3.gif' onclick="javascript:inout();" />
		    <input type='image' src='applications/<?=$appname;?>/_images/new_file.png' onclick="javascript:inout();" />
		    <input type='image' src='applications/<?=$appname;?>/_images/mini_down.gif' onclick="javascript:inout();" />
		    <input type='image' src='applications/<?=$appname;?>/_images/mini_up.gif' onclick="javascript:inout();" />
		    <input type='image' src='applications/<?=$appname;?>/_images/icon3.gif' onclick="javascript:inout();" />

                <div id="treeboxbox_tree" style="width: 250; height: 480;background-color:#f5f5f5;border :1px solid Silver;; overflow:auto;"></div>
			</td>
			<td valign="top" width="100%">



</p>
	<script>
			tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0);
			tree.setImagePath("js/dhtmlxTree/imgs/");

            function tonclick(id){
                var ob;
                for (i=0;i<99;i++){
                    ob=document.getElementById(i);
                    if (ob)ob.style.display="none";
                }

                ob=document.getElementById(id);
                if (ob)ob.style.display="inline";
                //alert (id);
            }
	       tree.setOnClickHandler(tonclick);

	</script>

<?php

	//$lista=glob("applications/$appname/template/*");
	$fd=opendir("applications/$appname/template/");
	while (false !== ($nf= readdir($fd))){
	    if ($nf[0]!='.' && $nf[0]!='_'  )
		$lista[$i++]=$nf;
	}
	closedir($fd);
	if (count($lista))sort($lista);

	echo "<script>tree.insertNewItem(0,1,\"sources\",0,0,0,0,\"SELECT\");</script>\n ";
	echo "<div id='1' style=\"display:none;border:2;\" >\n";
	echo "<h3 >add application</h3>";
	echo "<form name='newapp' action='index.php?appname=$appname&app-path=applications&name=&op=install' method='post'>\n";
	echo "<select name='addnew'>\n";
	foreach($lista as $elemento)
	    echo "<option value='$elemento'>".basename($elemento)."</option>\n";
	echo "</select>\n";
	echo "<input type='submit' value='install'>\n";
	echo "</form>\n";
	echo "</div>\n";

    if (!file_exists("datas/sources")) {mkdir("datas/sources",0755);}
    if (!file_exists("datas/sources/php")) {mkdir("datas/sources/php",0755);}
    if (!file_exists("datas/sources/asp")) {mkdir("datas/sources/asp",0755);}
    if (!file_exists("datas/sources/cpp")) {mkdir("datas/sources/cpp",0755);}
    if (!file_exists("datas/sources/c#")) {mkdir("datas/sources/c#",0755);}
    if (!file_exists("datas/sources/css")) {mkdir("datas/sources/css",0755);}
    if (!file_exists("datas/sources/delphi")) {mkdir("datas/sources/delphi",0755);}
    if (!file_exists("datas/sources/html")) {mkdir("datas/sources/html",0755);}
    if (!file_exists("datas/sources/javascript")) {mkdir("datas/sources/javascript",0755);}
    if (!file_exists("datas/sources/java")) {mkdir("datas/sources/java",0755);}
    if (!file_exists("datas/sources/perl")) {mkdir("datas/sources/perl",0755);}
    if (!file_exists("datas/sources/pyton")) {mkdir("datas/sources/pyton",0755);}
    if (!file_exists("datas/sources/ruby")) {mkdir("datas/sources/ruby",0755);}
    if (!file_exists("datas/sources/text")) {mkdir("datas/sources/text",0755);}
    if (!file_exists("datas/sources/generic")) {mkdir("datas/sources/generic",0755);}
	dirconfig("datas/sources/",1,$appname);
	echo "<script>tonclick(0);</script>";


	$op=$_GET['op'];

	if ($op=="change"){
	    $name=$_GET['name'];
	    $app_path=$_POST['app-path'];
	    $app_newpath=$_POST['app-newpath'];

	    $hidden=$_POST['hidden'];
	    $order=$_POST['order'];
	    $application=$_POST['application'];

	    echo "OP:$op<br/>\n\n";
	    echo "path:$app_path<br/>\n";
	    echo "newpath:$app_newpath<br/>\n";
	    echo "applicazione:$application<br/>\n";
	    echo "nascosto:$hidden<br/>\n";
	    echo "ordine:$order<br/>\n";
	    echo "file:$name<br/>\n";

	    if ($hidden==''){
		if ($order==''){
		    $newname=$application;
		}
		else{
		    $newname=$order."_".$application;
		}

	    }
	    else{
		if ($order==''){
		    $newname="_".$application;
		}
		else{
		    $newname="_".$order."_".$application;
		}
	    }
	    echo "nuovo nome:$newname<br/>\n";
	    rename("$app_path/$name","$app_newpath/$newname");
	    echo "<h3 class='edit'><a href='index.php?appname=$container&subapp=$subapp'>"._CONTINUA."</a></h3>";

	}

	if ($op=="del"){
	    $name=$_GET['name'];
	    $app_path=$_GET['app-path'];
	    echo "".$app_path.$name." eliminata";
	    deltree("".$app_path.$name);
	    echo "<h3 class='edit'><a href='index.php?appname=$container&subapp=$subapp'>"._CONTINUA."</a></h3>";
	}

	if ($op=="install"){
	    $name=$_GET['name'];
	    $app_path=$_GET['app-path'];
	    $addnew=$_POST['addnew'];
	    echo "applicazione $app_path.$name/".basename($addnew)." installata<br/>" ;
	    if (is_dir($addnew))
		copytree("$addnew",$app_path.$name."/".basename($addnew));
	    else
		copy("$addnew",$app_path.$name."/".basename($addnew));

	    echo "<h3 class='edit'><a href='index.php?appname=$container&subapp=$subapp'>"._CONTINUA."</a></h3>";
	}

	if ($op=="save"){
        $myfile=$_POST['myfile'];
        $file=$_GET['file'];
        echo $file." salvato<br>";
//        echo $myfile."<br>";
        $fp=fopen($file,"w");
           fwrite($fp, $myfile);
        fclose($fp);
        echo "Operazione di salvataggio riuscita !!!<br>";
	    echo "<h3 class='edit'><a href='index.php?appname=$container&subapp=$subapp'>"._CONTINUA."</a></h3>";
	}

?>

	    </td>
	</tr>
    </table>
    <?php
    }else{
    ?>
	<p><?=_ADMINISTRATION_PRIVILEGE_REQUESTED?> </p>
    <?php } ?>
<hr/>

